package com.cltech.controller.sys;

import com.cltech.dto.AuthorizationDto;
import com.cltech.dto.SysPermissionDto;
import com.cltech.entity.sys.SysPermission;
import com.cltech.frame.result.PageResult;
import com.cltech.frame.result.ResponseBean;
import com.cltech.service.sys.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by Administrator on 2019/4/14.
 */
@RestController
@RequestMapping("api/permission")
@Api(tags = {"权限资源管理"})
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;
    @PostMapping
    @ApiOperation("新增或编辑角色")
    public Object addOrUpdate(@RequestBody @Valid SysPermissionDto sysPermissionDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseBean.fail(bindingResult.getFieldError().getDefaultMessage());
        } else {
            return sysPermissionService.addOrUpdate(sysPermissionDto);
        }
    }
    @GetMapping
    @ApiOperation("权限资源列表查询")
    public Object list(HttpServletRequest request, @RequestParam(value="page",defaultValue="0")int page,
                       @RequestParam(value="size",defaultValue="10")int size) {
        return ResponseBean.success(PageResult.result(sysPermissionService.findAll(request.getParameterMap()
                ,page, size)));
    }
    @GetMapping("/permissionTree")
    @ApiOperation("权限资源树")
    public Object permissionTree(HttpServletRequest request) {
        return sysPermissionService.permissionTree(request.getParameterMap());
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id获取权限资源")
    public Object get(@PathVariable String id) {
        return sysPermissionService.findById(id);
    }


    @ApiOperation("删除权限资源")
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable String id) {
        sysPermissionService.deleteById(id);
        return ResponseBean.success("删除成功");
    }
}
